package com.huike.web.controller.system;

import com.huike.clues.domain.dto.SysDictTypeCreateDTO;
import com.huike.clues.domain.dto.SysDictTypePageDTO;
import com.huike.clues.domain.entity.SysDictType;
import com.huike.clues.domain.vo.DictTypeListVO;
import com.huike.clues.domain.vo.OptionselectVO;
import com.huike.clues.domain.vo.SysDictTypePageVO;
import com.huike.clues.service.ISysDictTypeService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 数据字典类型信息相关接口
 * @Author zhuzi
 * @Date 2023/10/15
 */
@RequestMapping("/system/dict/type")
@RequiredArgsConstructor
@RestController
@Slf4j
public class SysDictTypeController {

    private final ISysDictTypeService service;

    /**
     * 新增字典类型
     */
    @PostMapping
    public AjaxResult<?> create(@RequestBody SysDictTypeCreateDTO dto) {
        log.info("Get SysDictTypeCreateDTO: {}", dto);
        service.createDictType(dto);
        return AjaxResult.success();
    }

    /**
     * 更新字典类型
     */
    @PutMapping
    public AjaxResult<?> update(@RequestBody SysDictType dictType) {
        log.info("Get SysDictType: {}", dictType);
        service.updateDictType(dictType);
        return AjaxResult.success();
    }

    /**
     * 清空缓存
     */
    @DeleteMapping("/clearCache")
    public AjaxResult<?> clearCache() {
        service.clearCache();
        return AjaxResult.success();
    }

    /**
     * 字典类型分页查询
     */
    @GetMapping("/list")
    public TableDataInfo<List<DictTypeListVO>> getDictTypeList(SysDictTypePageDTO dto) {
        log.info("Get dto: {}", dto);
        return service.pageList(dto);
    }

    /**
     * 根据字典类型id获取字典类型信息
     *
     * @param dictId 字典类型id
     * @return 字典类型信息
     */
    @GetMapping("/{dictId}")
    public AjaxResult<SysDictType> getDictTypeById(@PathVariable Long dictId) {
        log.info("Get dictId: {}", dictId);
        return AjaxResult.success(service.getDictTypeById(dictId));
    }

    /**
     * 批量删除字典类型
     *
     * @param dictIds 字典类型id集合
     */
    @DeleteMapping("/{dictIds}")
    public AjaxResult<?> delete(@PathVariable List<Long> dictIds) {
        log.info("Get dictIds: {}", dictIds);
        service.delete(dictIds);
        return AjaxResult.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult<List<OptionselectVO>> optionselect() {
        // 这是什么b接口???
        return AjaxResult.success(service.optionselect());
    }

    /**
     * 导出表格
     */
    @GetMapping("/export")
    public AjaxResult<?> export(SysDictTypePageDTO dto) {
        List<SysDictType> list = service.getDictTypeList2(dto);
        ExcelUtil<SysDictType> util = new ExcelUtil<>(SysDictType.class);
        return util.exportExcel(list, "字典类型");
    }

}
